package com.tri.framework;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager 
{
	private SoundPool m_soundPool;
	private HashMap<Integer, Integer> m_soundPoolMap;
	private AudioManager m_audioManager;
	private Context m_activity;
	
	private static SoundManager s_instance;
	
	public static SoundManager getInstance()
	{
		if(s_instance == null)
		{
			s_instance = new SoundManager();
		}
		return s_instance;
	}
	
	public void Init(Context context)
	{
		m_soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
		m_soundPoolMap = new HashMap<Integer, Integer>();
		m_audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
		m_activity = context;
					
	}
	
	public void addSound(int index, int soundID)
	{
		int id = m_soundPool.load(m_activity, soundID, 1);
		m_soundPoolMap.put(index, id);
	}
	
	public void play(int index)
	{
		float streamVolume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		streamVolume = streamVolume / m_audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		m_soundPool.play((Integer)m_soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
	}

	public void playLooped(int index)
	{
		float streamVolume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		streamVolume = streamVolume / m_audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		m_soundPool.play((Integer)m_soundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
	}
}
